package com.frxuexi.park2212.merchant

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.MerchantBeanItem
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityMerchantBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 商家入驻
 */
@Route(path = "/app/MerchantActivity")
@AndroidEntryPoint
class MerchantActivity : BaseActivity<ActivityMerchantBinding, MerchantViewModel>() {
    val merchantAdapter by lazy { MerchantAdapter() }
    val list = mutableListOf<MerchantBeanItem>()
    override fun initData() {
        vm.getMerchants.observe(this) {
            list.clear()
            merchantAdapter.data.clear()
            list.addAll(it)
            merchantAdapter.addData(it.filter { it.status == "新建" })
            merchantAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.headMerchant.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("入驻管理")
            headerAdd.setOnClickListener {
                ARouter.getInstance().build("/app/MerchantAddActivity").navigation()
            }
        }
        binding.rvMerchant.apply {
            adapter = merchantAdapter
            layoutManager = LinearLayoutManager(this@MerchantActivity)
        }

        binding.merchantRvstatus.setOnCheckedChangeListener { group, checkedId ->
            merchantAdapter.data.clear()
            if (checkedId == R.id.merchant_a) {
                merchantAdapter.addData(list.filter { it.status == "新建" })
            } else {
                merchantAdapter.addData(list.filter { it.status != "新建" })
            }
            merchantAdapter.notifyDataSetChanged()
        }

        merchantAdapter.addChildClickViewIds(R.id.merchant_status)
        merchantAdapter.setOnItemChildClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/MerchantDetailActivity")
                .withString("mid", merchantAdapter.data.get(position).id.toString()).navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getMerchants()
        binding.merchantRvstatus.check(R.id.merchant_a)
    }

}